import 'uno.css'
import '@/design/index.less'
import 'ant-design-vue/dist/reset.css'
import 'vant/lib/index.css'
import './global.css'
import './css/variable.scss'
import './css/common.scss'
// Register icon sprite
import 'virtual:svg-icons-register'
import { createApp } from 'vue'
import { registerGlobComp } from '@/components/registerGlobComp'
import { setupGlobDirectives } from '@/directives'
import { setupI18n } from '@/locales/setupI18n'
import { setupErrorHandle } from '@/logics/error-handle'
import { initAppConfigStore } from '@/logics/initAppConfig'
import { router, setupRouter } from '@/router'
import { setupRouterGuard } from '@/router/guard'
import { setupStore } from '@/store'
// import DataVVue3 from '@kjgl77/datav-vue3'
import ant from 'ant-design-vue'
import { renderWithQiankun, qiankunWindow } from 'vite-plugin-qiankun/dist/helper'

import App from './App.vue'

let app: any = null

async function bootstrapVue3(container) {
  app = createApp(App)
  // app.use(DataVVue3)
  app.use(ant)
  // Configure store
  // 配置 store
  setupStore(app)

  // Initialize internal system configuration
  // 初始化内部系统配置
  initAppConfigStore()

  // Register global components
  // 注册全局组件
  registerGlobComp(app)

  // Multilingual configuration
  // 多语言配置
  // Asynchronous case: language files may be obtained from the server side
  // 异步案例：语言文件可能从服务器端获取
  await setupI18n(app)

  // Configure routing
  // 配置路由
  setupRouter(app)

  // router-guard
  // 路由守卫
  setupRouterGuard(router)

  // Register global directive
  // 注册全局指令
  setupGlobDirectives(app)

  // Configure global error handling
  // 配置全局错误处理
  setupErrorHandle(app)

  // https://next.router.vuejs.org/api/#isready
  // await router.isReady();

  app.mount(container)
}

// bootstrapVue3()

const initQianKun = () => {
  console.log('处于子应用模式...')

  renderWithQiankun({
    // 当前应用在主应用中的生命周期
    // 文档 https://qiankun.umijs.org/zh/guide/getting-started#
    mount(props) {
      console.log('b20 ziems 子应用 收到的 props:', props)
      bootstrapVue3(props.container?.querySelector('#app'))
      //  可以通过props读取主应用的参数：msg
      // 监听主应用传值以及我本身的一些业务逻辑
      props.setLoading(false)
      props.onGlobalStateChange((res) => {
        console.log('res:', res)
      })
    },
    bootstrap() {
      console.log('bootstrap')
      // 做一些一次性的初始化的工作，因为只触发一次
    },
    update() {
      console.log('update')
    },
    unmount(props) {
      // 卸载子应用
      app.unmount()
      app = null
    },
  })
}

// 判断当前应用是否在主应用中
qiankunWindow.__POWERED_BY_QIANKUN__ ? initQianKun() : bootstrapVue3(document.getElementById('app'))
